package com.vipheyue.time;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends Activity {
    private AudioManager _audioManager;
    private int _cachedVolume;
    private int _maxVolume;
    public static final int AUDIO_TYPE = AudioManager.STREAM_RING;
//            public static final int AUDIO_TYPE = AudioManager.STREAM_SYSTEM;
    private IVoice ivoice;
    VoiceServiceConnection conn = new VoiceServiceConnection();
    MyVolumeReceiver mVolumeReceiver;
    int cacheVoice;
    Boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        myRegisterReceiver();
        cacheVoice =4;
        _audioManager.setStreamVolume(AUDIO_TYPE, cacheVoice, 0); //tempVolume:音量绝对值
//        cacheVoice = _audioManager.getStreamVolume(AUDIO_TYPE);//用于比较判断是用户按的是什么
        Log.i("MainActivity", "进入系统音量....." + cacheVoice);

    }
/********************************************/
    /**
     * 处理音量变化时的界面显示
     *
     * @author long
     */
    private class MyVolumeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
                int currVolume = _audioManager.getStreamVolume(AUDIO_TYPE);//
                Log.i("MainActivity", "当前音量....." + currVolume);
                final Calendar mCalendar = Calendar.getInstance();
                int mMinuts = mCalendar.get(Calendar.MINUTE);
                int mHour = mCalendar.get(Calendar.HOUR);
                Log.i("MainActivity", "cacheVoice:....." + cacheVoice+"...currVolume:"+currVolume);
                if (currVolume <cacheVoice) {
                    mMinuts = mMinuts - 1;
                }
                if (currVolume > cacheVoice) {
                    mMinuts = mMinuts + 1;
                }
                Log.i("MainActivity", "mMinuts....." + mMinuts);


                try {
                    SystemDateTime.setTime(mHour, mMinuts);
//                    cacheVoice = currVolume;
                    //取消监听
                    unregisterReceiver(mVolumeReceiver);
                    //重置音量
                    _audioManager.setStreamVolume(AUDIO_TYPE, cacheVoice, 0); //tempVolume:音量绝对值

                    //开始监听
                    myRegisterReceiver();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this, "特殊时间", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    /**
     * 注册当音量发生变化时接收的广播
     */
    private void myRegisterReceiver() {
        mVolumeReceiver = new MyVolumeReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.media.VOLUME_CHANGED_ACTION");
        registerReceiver(mVolumeReceiver, filter);
    }

    /********************************************/


    public void alter(View view) {
        _audioManager.setStreamVolume(AUDIO_TYPE, cacheVoice, 0); //tempVolume:音量绝对值
        Toast.makeText(MainActivity.this, "开启成功", Toast.LENGTH_SHORT).show();
//        Intent service = new Intent(this, MyService.class);
//        Boolean flag = bindService(service, conn, BIND_AUTO_CREATE);//绑定成功后,系统自动调用conn里面的方法
//        _maxVolume = _audioManager.getStreamMaxVolume(AUDIO_TYPE);
//        _cachedVolume = _audioManager.getStreamVolume(AUDIO_TYPE);
//        Toast.makeText(MainActivity.this, "_cachedVolume:" + _cachedVolume, Toast.LENGTH_SHORT).show();
//        Log.i("MainActivity", "_maxVolume" + _maxVolume);
//        Log.i("MainActivity", "_cachedVolume" + _cachedVolume);
//        try {
//            SystemDateTime.setTime(8,55);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    private class VoiceServiceConnection implements ServiceConnection {

        public void onServiceConnected(ComponentName name, IBinder service) {
            ivoice = (IVoice) service;
            ivoice.queryVoice(_audioManager);
        }

        public void onServiceDisconnected(ComponentName name) {
            ivoice = null;
        }

    }
}
